package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContextUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequiredPermission(name = "公告页面", expression = "notice:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo) {
        //查询以读的公告
        Employee employee = UserContextUtil.getUserContext();
        qo.setAdmin(employee.isAdmin());
        qo.setId(employee.getId());
        List<Long> readList = noticeService.markReadAll(employee.getId());
        if (qo.getSeekRead() != null) {
            qo.setRead(readList);
        }
        model.addAttribute("markReadAll", readList);
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("PageInfo", pageInfo);
        return "notice/list";
    }


    @RequiredPermission(name = "公告删除", expression = "notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name = "公告新增/编辑", expression = "notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {
        if (notice.getId() == null) {
            notice.setCreation(UserContextUtil.getUserContext());
            notice.setCreationTime(new Date());
            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name = "公告新增/编辑", expression = "notice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("notice", noticeService.get(id));
        }
        return "notice/input";
    }

    @RequestMapping("/show")
    public String show(Long id, Model model,NoticeQueryObject qo) {
        noticeService.insertReadStatus(UserContextUtil.getUserContext().getId(), id);
        model.addAttribute("notice", noticeService.get(id));
        //查询出这个用户已读的公告
        noticeService.noticeHin(qo);
        return "notice/show";
    }

    //改变发布状态
    @RequestMapping("/alterStatus")
    public String alterStatus(Notice notice) {
        noticeService.alterStatus(notice);
        return "redirect:/notice/list";
    }
}
